#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define err_quit(msg) \
  do { \
    fprintf(stderr, msg": %d\n", errno);              \
  exit(EXIT_FAILURE); \
  } while (0);

#define MAXLINE 200

int
main(int argc, char *argv[])
{
  int listenfd, connfd;
  struct sockaddr_in servaddr;
  char buff[MAXLINE];

  if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    err_quit("listen socket error");

  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  servaddr.sin_port = htons(8585);

  if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    err_quit("bind failed");

  if (listen(listenfd, 0) < 0)
    err_quit("listen failed");
  for (;;)
    {
      connfd = accept(listenfd, NULL, NULL);
      snprintf(buff, sizeof(buff), "Get a fuck out\r\n");
      write(connfd, buff, strlen(buff));
      close(connfd);
    }
  close(listenfd);

  return EXIT_SUCCESS;
}
